package com.yupi.yuoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yuoj.common.BaseResponse;
import com.yupi.yuoj.common.DeleteRequest;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.common.ResultUtils;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.exception.ThrowUtils;
import com.yupi.yuoj.model.dto.learninghistory.LearningHistoryAddRequest;
import com.yupi.yuoj.model.dto.learninghistory.LearningHistoryQueryRequest;
import com.yupi.yuoj.model.entity.LearningHistory;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.model.vo.LearningHistoryVO;
import com.yupi.yuoj.service.LearningHistoryService;
import com.yupi.yuoj.service.UserService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 学习历史接口
 *
 * @author jingjin
 */
@RestController
@RequestMapping("/learning_history")
@Slf4j
public class LearningHistoryController {

    @Resource
    private LearningHistoryService learningHistoryService;

    @Resource
    private UserService userService;

    /**
     * 创建学习历史记录
     *
     * @param learningHistoryAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addLearningHistory(@RequestBody LearningHistoryAddRequest learningHistoryAddRequest, HttpServletRequest request) {
        if (learningHistoryAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LearningHistory learningHistory = new LearningHistory();
        BeanUtils.copyProperties(learningHistoryAddRequest, learningHistory);
        learningHistoryService.validLearningHistory(learningHistory, true);
        User loginUser = userService.getLoginUser(request);
        learningHistory.setUser_id(loginUser.getId());
        boolean result = learningHistoryService.save(learningHistory);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        long newLearningHistoryId = learningHistory.getId();
        return ResultUtils.success(newLearningHistoryId);
    }

    /**
     * 删除学习历史记录
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteLearningHistory(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        LearningHistory oldLearningHistory = learningHistoryService.getById(id);
        ThrowUtils.throwIf(oldLearningHistory == null, ErrorCode.NOT_FOUND_ERROR);
        // 仅本人或管理员可删除
        if (!oldLearningHistory.getUser_id().equals(user.getId()) && !userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean b = learningHistoryService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<LearningHistoryVO> getLearningHistoryVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LearningHistory learningHistory = learningHistoryService.getById(id);
        if (learningHistory == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(learningHistoryService.getLearningHistoryVO(learningHistory, request));
    }

    /**
     * 分页获取学习历史列表（封装类）
     *
     * @param learningHistoryQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<LearningHistoryVO>> listLearningHistoryVOByPage(@RequestBody LearningHistoryQueryRequest learningHistoryQueryRequest,
            HttpServletRequest request) {
        long current = learningHistoryQueryRequest.getCurrent();
        long size = learningHistoryQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<LearningHistory> learningHistoryPage = learningHistoryService.page(new Page<>(current, size),
                learningHistoryService.getQueryWrapper(learningHistoryQueryRequest));
        return ResultUtils.success(learningHistoryService.getLearningHistoryVOPage(learningHistoryPage, request));
    }

    /**
     * 分页获取当前用户的学习历史列表
     *
     * @param learningHistoryQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<LearningHistoryVO>> listMyLearningHistoryVOByPage(@RequestBody LearningHistoryQueryRequest learningHistoryQueryRequest,
            HttpServletRequest request) {
        if (learningHistoryQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        learningHistoryQueryRequest.setUserId(loginUser.getId());
        long current = learningHistoryQueryRequest.getCurrent();
        long size = learningHistoryQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<LearningHistory> learningHistoryPage = learningHistoryService.page(new Page<>(current, size),
                learningHistoryService.getQueryWrapper(learningHistoryQueryRequest));
        return ResultUtils.success(learningHistoryService.getLearningHistoryVOPage(learningHistoryPage, request));
    }

}